﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BoggleGUI
{
    public delegate void ClickedHandler(object sender, EventArgs e);
    public delegate void AlreadyClickedHandler(object sender, EventArgs e);
    public delegate void RightClickHandler(object sender, EventArgs e);

    /// <summary>
    /// Interaction logic for DisplayGrid.xaml
    /// </summary>
    public partial class DisplayGrid : UserControl
    {
        //SolidColorBrush maroonBrush = new SolidColorBrush(Colors.Maroon);
        //SolidColorBrush blackBrush = new SolidColorBrush(Colors.Black);
        //SolidColorBrush grayBrush = new SolidColorBrush(Colors.Gray);
        //SolidColorBrush redBrush = new SolidColorBrush(Colors.Red);
        
        private List<BoggleCell> clickedCells = new List<BoggleCell>();

        public event ClickedHandler Clicked;
        public event ClickedHandler AlreadyClicked;
        public event RightClickHandler RightClick;
        
        public static readonly DependencyProperty BoardLayoutProperty =
            DependencyProperty.Register("Layout", typeof(string), typeof(DisplayGrid));                
        
        public string BoardLayout 
        {
            get { return (string)this.GetValue(BoardLayoutProperty); }

            set
            {
                this.SetValue(BoardLayoutProperty, value);
                SetLayout(value);
                layout = value;
            }
        }

        private string layout;

        private BoggleCell[,] cells = new BoggleCell[4,4];

        public DisplayGrid()
        {
            InitializeComponent();
            InitializeGrid();
        }

        private void InitializeGrid()
        {
            for (int col = 0; col < 4; col++)
            {
                for (int row = 0; row < 4; row++)
                {
                    // create a new boggle cell and add it to the collection
                    BoggleCell cell = new BoggleCell(col, row);
                    cells[col, row] = cell;

                    // set the label to ' '
                    cell.SetLabel(' ');

                    cell.CellClicked += new BoggleCell.CellClickedHandler(OnCellClicked);

                    // position the cell on the canvas
                    Canvas.SetLeft(cell, col * 75 + 2.5);
                    Canvas.SetTop(cell, row * 75 + 2.5);

                    // make the cell visible
                    GridCanvas.Children.Add(cell);
                }
            }
        }

        private void OnCellClicked(object sender, EventArgs e)
        {
            BoggleCell cell = (BoggleCell)sender;

            if (clickedCells.Count == 0 || cell.IsValidClick(clickedCells.Last()))
            {
                if (Clicked != null && !clickedCells.Contains(cell))
                {
                    Clicked(sender, null);
                    clickedCells.Add(cell);
                    cell.rect2.Visibility = Visibility.Visible;
                }
                else if (AlreadyClicked != null && clickedCells.Last() == cell)
                {
                    AlreadyClicked(sender, null);
                    clickedCells.Remove(cell);
                    cell.rect2.Visibility = Visibility.Hidden;
                }
            }
            else
            { 
            
            }
        }

        private void SetLayout(string s)
        {
            if (s.Length > 0)
            {
                int index;

                for (int col = 0; col < 4; col++)
                {
                    for (int row = 0; row < 4; row++)
                    {
                        index = col + row * 4;

                        cells[col, row].SetLabel(s[index]);
                    }
                }
            }
        }

        public void ClearSelection()
        {
            foreach (BoggleCell c in cells)
            {
                c.ResetPiece();
            }

            clickedCells.Clear();
        }

        private void GridCanvas_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
            ClearSelection();

            clickedCells.Clear();

            if (RightClick != null)
            {
                RightClick(this, null);
            }
        }
    }
}
